package com.ha.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ha.constant.MessageConstant;
import com.ha.constant.PasswordConstant;
import com.ha.constant.StatusConstant;
import com.ha.dto.UserLoginDTO;
import com.ha.dto.UserPageDTO;
import com.ha.dto.UserRegisterDTO;
import com.ha.entity.Employee;
import com.ha.entity.User;
import com.ha.exception.AccountLockedException;
import com.ha.exception.AccountNotFoundException;
import com.ha.exception.BaseException;
import com.ha.exception.PasswordErrorException;
import com.ha.mapper.UserMapper;
import com.ha.result.PageResult;
import com.ha.service.UserService;
import com.ha.vo.GarbageVO;
import com.ha.vo.KindIdVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: UserServiceImpl
 * Package: com.ha.service.impl
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/4/22  20:53
 * @Version 1.0
 */
@Service
@Slf4j
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult pageQuery(UserPageDTO userPageDTO) {
        //开始分页查询,动态添加limit 和计算 来实现查询,基于拦截器
        PageHelper.startPage(userPageDTO.getPage(),userPageDTO.getPageSize());
        Page<User> page = userMapper.pageQuery(userPageDTO);
        long total = page.getTotal();
        List<User> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void addUser(UserRegisterDTO userRegisterDTO) {
        if(userRegisterDTO != null ){
            User user1 = userMapper.getByUsername(userRegisterDTO.getUsername());
            if(user1 != null){
                throw new BaseException("该账号已存在，请重新输入账号");
            }

            User user = new User();

            BeanUtils.copyProperties(userRegisterDTO,user);

            if(userRegisterDTO.getPassword() == null || userRegisterDTO.getPassword() == ""){
                user.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
            }else {
                user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
            }

            user.setCreditPoint(0);
            user.setMaxCreditPoint(0);
            user.setDeleted(1);
            log.info("开始添加数据:{}",user);
            userMapper.addUser(user);
        }else {
            throw new BaseException("未添加数据");
        }
    }

    @Override
    public void updateUser(UserRegisterDTO userRegisterDTO) {
        if(userRegisterDTO !=null ){
            if(userMapper.getByUsername(userRegisterDTO.getUsername()) == null){
                throw new BaseException("用户名不允许修改");
            }else {
                User user = new User();
                BeanUtils.copyProperties(userRegisterDTO,user);
                if(user.getPassword()!=null && user.getPassword()!= ""){
                    user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
                }

                userMapper.update(user);
            }


        }else {
            throw new BaseException("更新数据输入有误");
        }


    }

    @Override
    public void delete(Integer deleted,Long id) {
        if(userMapper.getById(id) != null ){
            userMapper.delete(deleted,id);
        }else {
            throw new BaseException("要删除的用户不存在");
        }
    }

    @Override
    public User getById(Long id) {
        if(id != null){
            return userMapper.getById(id);
        }else {
            throw new BaseException("没有该用户");
        }
    }

    /**
     *用户登录
     * @param userLoginDTO
     * @return
     */
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        //查询数据库
        User user =  userMapper.getByUsername(username);
//不存在用户
        if(user == null ){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //存在账户，密码用md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
//密码不相等
        if(!password.equals(user.getPassword())){

            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //密码相等，账号已锁定
        if (user.getDeleted() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
//全满足后返回实体
        return user;
    }
}
